import json

import requests


def getComments(id):
    params = {
        "limit":20,
        # offset: 3
    }
    headers = {
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        "referer":"https://www.zhihu.com/question/"+id
    }
    url = "https://www.zhihu.com/api/v4/questions/"+id+"/answers"
    res = requests.get(url,params,headers=headers)
    data = json.loads(res.text)['data']
    # print(data)
    for i,item in enumerate(data):
        print(item)
        d =item["content"]
        print(d)
# def getUrl():
#     url = "https://www.zhihu.com/api/v4/search_v3"
#     headers = {
#         "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
#         "referer":"https://www.zhihu.com/search?type=content&q=%E7%A8%8B%E5%BA%8F%E5%91%98%E5%96%9C%E6%AC%A2%E4%BB%80%E4%B9%88%E6%A0%B7%E5%AD%90%E5%A5%B3%E7%94%9F"
#     }
#     params = {
#         "t": "general",
#         # "type": "content",
#         "q": "程序员喜欢女生",
#         "offset":0,#0~20  #40~20
#         "limit": 20,
#         "lc_idx":0,
#     }
#     res = requests.get(url,params,headers=headers)
#     data = json.loads(res.text)['data']
#     # print(data)
#     for i,item in enumerate(data):
#         # print(i)
#         if(i==0):
#             continue
#         try:
#             d = item["object"]["question"]["name"]
#             id =  item["object"]["question"]["id"]
#         except KeyError:
#             d = item["object"]["title"]
#             id =  item["object"]["id"]
#         if "程序" in d and "喜欢" in d and ("女生"  in d or "妹子"  in d or "女孩子"  in d):
#             print(d.replace("<em>","").replace("</em>",""))
#             print(id)
#             getComments(id)
#

        # print(d.replace("<em>","").replace("</em>",""))
    # print(res.text)
# getUrl()
getComments("48104276")